<?php
class Goods {
    private $db = null;
    private $dsn;
    private $user;
    private $pwd;
    
    // 初始化
    function __construct(string $dbname, string $user, string $pwd)
    {
        $dsn = 'mysql:dbname='.$dbname.';host=127.0.0.1';
        $this->$dsn = $dsn;
        $this->$user = $user;
        $this->$pwd = $pwd;
        try {
            $this->db = new PDO($dsn, $user, $pwd);
        } catch (Exception $e) {
            echo 'Exception Message:'.$e->getMessage();
        }
        return $this;  
    }

    function __destruct()
    {
        if ($this->db) {
            $this->db = null;
        }
    }

    function reconncet()
    {
        try {
            $this->db = new PDO($this->dsn, $this->user, $this->pwd);
            return false;
        } catch (Exception $e) {
            echo 'Reconnect failed:'.$e->getMessage();
        }
        return true;
    }


    // 显示商品
    function getData() 
    {
        if ($this->db == null) {
            echo 'DB not connect!<br/>';
            die;
        }
        $sql = 'select * from `goods`';
        $result = $this->db->query($sql);

        $arr = array();
        foreach ($result as $row) {
            $arr_ = array(
                'id'=>$row['id'],
                'name'=>$row['name'],
                'salary'=>$row['salary'],
                'number'=>$row['number']
            ); 
            
            $arr[] = $arr_;
        }
        return $arr;
    }

    // 从商品仓库中删除
    function delCount(string $name, int $count)
    {
        if ($this->db == null) {
            if ($this->reconncet() == false)
                die;
        }
        $sql = "select * from `goods`";
        $result = $this->db->query($sql);
        $num = 0;
        $found = false;
        foreach ($result as $row) {
            if ($row['name'] == $name) {
                $num = $row['number'];
                $found = true;
                break;
            }
        }
        if ($found == false) {
            return false;
        }
        $left_num = $num - $count;
        if ($left_num < 0) {
            $left_num = 0;
        }
        if ($left_num == 0) {
            // 删除该商品
            $sql = "delete from `goods` where name = '{$name}'";
            $result = $this->db->exec($sql);
            if ($result == 0) {
                return false;
            }
        } else {
            // 更新商品数量
            $sql = "update goods set number={$left_num} where name = '{$name}'";
            $result = $this->db->exec($sql);
            if ($result == 0) {   
                return false;
            }
        }
        return true;
    }

    // 添加到商品仓库
    function insert(string $name, float $salary, int $number)
    {
        $sql = "INSERT INTO goods (`name`, `salary`, `number`) 
            VALUES ('".$name."', ".$salary.", ".$number.")";
        if ($this->db->exec($sql) == 0) {
            echo 'Insert Failed:'.$this->db->errorCode().'<br>';
            return false;
        }
        return true;
    }
    // 从商品仓库中删除
    function delect(string $name)
    {
        $sql = "DELECT FROM goods WHERE `name` == ".$name;
        if ($this->db->exec($sql) == 0) {
            echo 'Delect Failed:'.$this->db->errorCode().'<br>';
            return false;
        }
        return true;
    }

};